var config = require('./utils/config.js');//引入基础配置文件
App({
  globalData: {
    token: null,
    user_info: null,
    weight_user_sync: false,
  },
  //每次打开就判断token
  onShow: function (options) {
    let token = wx.getStorageSync('token');
    if (!token) {
      //没有缓存则去登录授权
      this.login(this.getUserInfo);
    } else {
      //验证token是否还有效果
      this.checkToken(token);
    }
  },
  //页面刷新
  onPullDownRefresh: function () {
    var that = this;
    console.log("用户下拉")
    wx.stopPullDownRefresh();
  },
  //验证token
  checkToken: function (token) {
    let that = this;
    wx.request({
      url: config.host + '/assistant/wxregister/check_token',
      data: { token: token },
      success: function (res) {
        if (res.data.code != 0) {
          that.login(that.getUserInfo);
        } else {
          that.globalData.token = token;
          //that.globalData.user_info = wx.getStorageSync('user_info');
        }
      }
    })
  },
  //用户登录
  login: function (callback) {
    let that = this;
    wx.login({
      success: function (res) {
        wx.request({
          url: config.host + '/assistant/Wxregister/register',
          data: { code: res.code },
          success: function (res) {
            //其他地方授权 登录
            if (res.data.code == 0) {
              that.globalData.token = res.data.token;
              wx.setStorageSync('token', res.data.token);
              wx.setStorageSync('user', res.data.data.user_info);
            } else if (res.data.code == 260) {
              callback();
            }
          }
        })
      }
    })
  },
  // 获取设备信息
  getAdvice: function () {
    return wx.getSystemInfoSync();
  },
  //获取用户信息
  getUserInfo: function () {
    let that = this;
    wx.getUserInfo({
      success: function (res) {
        wx.login({
          success: function (re) {
            //发送code给开发服务器
            wx.request({
              url: config.host + '/assistant/Wxregister/register',
              data: { code: re.code, encryptedData: res.encryptedData, iv: res.iv },
              success: function (res) {
                if (res.data.code == 0) {
                  //存缓存
                  that.globalData.token = res.data.token;
                  wx.setStorageSync('token', res.data.token);
                  wx.setStorageSync('user', res.data.data.user_info);
                }
              }
            })
          }
        })
      }, fail: function (res) {
        //用户非授权
      }
    })
  },
  //推送用户信息给服务器
  pushInfo: function (data) {
    let that = this;
    if (data) {
      wx.request({
        url: config.host + '/assistant/Wxregister/save_wx_user_info',
        data: { token: that.globalData.token, nickname: data.nickName, headImg: data.avatarUrl, sex: data.gender, province: data.province, city: data.city, country: data.country },
        success: function (res) {
          console.log(res);
        }
      })
    }
  },

  //转发小程序(path页面路径)
  share_page: function (path, title) {
    let params = '';
    if (typeof path == 'undefined') {
      let pages = getCurrentPages();
      let _page = pages[pages.length - 1];
      path = _page.route;
      let options = _page.options;
      for (var index in options) {
        params += index + "=" + options[index] + "&";
      }
      params = params.substring(0, params.length - 1);
    }
    title = title ? title : "分享给好友吧";
    let user_info = wx.getStorageSync('user_addition_info');
    if (user_info) {
      return {
        'title': title,
        'path': path + '?q=_share_' + user_info.uid + '&' + params
      };
    } else {
      return {
        'title': title,
        'path': path + '?' + params
      };
    }
  },

  //提示
  hintTime: function (title, icon, page, mode) {
    wx.showToast({
      title: title,
      icon: icon,
      duration: 1500,
    })
    setTimeout(function () {
      if (typeof mode == 'undefined' || mode == 0) {
        wx.navigateTo({
          url: page,
        })
      } else {
        wx.switchTab({
          url: page,
        });
      }
    }, 1500);
  },
  hint: function (title, icon) {
    wx.showToast({
      title: title,
      icon: icon,
      duration: 1500,
    })
  },
})